<?php

namespace island\controllers;

use frontend\modules\common\models\BaseHelper;
use Yii;
use yii\db\ActiveRecord;


/**
 * Site controller
 */
class ClassicController extends BaseController
{
    public $enableCsrfValidation = false;

    /**
     * 旧岛小程序接口 最新的
     * @return string
     * @throws \yii\db\Exception
     */
    public function actionLatest()
    {

        $sql = "SELECT * FROM `island_resource`";
        $res = Yii::$app->db->createCommand($sql)->queryOne();
        $res['images'] = Yii::$app->params['staticHost'] . $res['images'];

        return json_encode($res);

    }

    public function actionLike()
    {
        $id = Yii::$app->request->post('art_id');

        $sql = "UPDATE `island_resource` SET like_status = 1,fav_nums = fav_nums+1 WHERE id = " . $id;
        $res = Yii::$app->db->createCommand($sql)->execute();

        return json_encode($res);

    }

    public function actionCancellike()
    {
        $id = Yii::$app->request->post('art_id');

        $sql = "UPDATE `island_resource` SET like_status = 0,fav_nums = fav_nums-1 WHERE id = " . $id;
        $res = Yii::$app->db->createCommand($sql)->execute();

        return json_encode($res);

    }

    public function actionPrevious()
    {

        $id = Yii::$app->request->get('id');

        $sql = "SELECT * FROM `island_resource`  WHERE `index` < " . $id . " LIMIT 1";
        $res = Yii::$app->db->createCommand($sql)->queryOne();

        $res['images'] = Yii::$app->params['staticHost'] . $res['images'];
        return json_encode($res);

    }

    public function actionNext()
    {

        $id = Yii::$app->request->get('id');

        $sql = "SELECT * FROM `island_resource`  WHERE `index` > " . $id . " ORDER BY id DESC LIMIT 1";
        $res = Yii::$app->db->createCommand($sql)->queryOne();

        $res['images'] = Yii::$app->params['staticHost']. $res['images'];
        return json_encode($res);

    }

    /**
     * 点赞情况
     */
    public function actionFavor(){

        $id = Yii::$app->request->get('id');

        $sql = "SELECT `like_status`,`fav_nums` FROM `island_resource` WHERE `id` = ".$id;
        $res = Yii::$app->db->createCommand($sql)->queryOne();

        return json_encode($res);
    }


}